package com.yonyou.brigade.openapi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.openapi.service.IUserCenterListenerService;
import com.yonyou.brigade.rtpnr.service.IRtpnrUserService;
import com.yonyou.brigade.third_parties.ctrip.bigcustomer.service.ICtripBigUserService;
import com.yonyou.brigade.third_parties.yihao.service.ICLYHUserService;
import com.yonyou.ybzboot.starter.common.exception.BaseException;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserCenterListenerServiceImpl implements IUserCenterListenerService {
    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService iBriChannelService;

    @Autowired
    @Qualifier("rtpnrUserServiceImpl")
    private IRtpnrUserService iRtpnrUserService;

    @Autowired
    @Qualifier("ctripBigUserServiceImpl")
    private ICtripBigUserService ctripBigUserService;

    @Autowired
    private ICLYHUserService iclyhUserService;

    private Logger logger=LoggerFactory.getLogger(UserCenterListenerServiceImpl.class);

    /**
     * 用户中心的用户新增的监听，
     * 一旦有用户新增，根据channel表中的开通情况，自动同步到对应的第三方服务
     *
     * @param tenantId
     * @param orgPk
     * @param ucUserDtoList
     * @return
     */
    @Override
    public void addUserList(String tenantId, String orgPk, List<UcUserDto> ucUserDtoList) {
        List<BriChannel> briChannels = getChannels(tenantId,orgPk);
        logger.info(tenantId+"获取到的开通频道数量为"+briChannels.size());
        briChannels.forEach(briChannel -> {
            String channel = briChannel.getChannel();
            try {
                if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
                    switch (channel){
                        case "rtpnr":
                            logger.info("开始向友商旅同步用户：");
                            iRtpnrUserService.syncUsersByUserCenterListener(tenantId,orgPk,ucUserDtoList);
                            logger.info("结束向友商旅同步用户：");
                            break;
                        case "ystrip":

                            break;
                        case "ctripBC":
                            logger.info("开始向携程同步用户：");
                            ctripBigUserService.syncCtripBigUsers(tenantId,orgPk,ucUserDtoList);
                            logger.info("结束向携程同步用户：");
                            break;
                        case "clyh":
                            logger.info("开始向差旅一号同步用户：");
                            iclyhUserService.sysClyhUsers(tenantId, orgPk, ucUserDtoList, briChannel);
                            logger.info("结束向差旅一号同步用户：");
                        default:
                            break;
                    }
                }
            } catch (Exception e) {
                logger.error("用户同步报错："+channel+":"+e.getMessage());
            }
        });
    }

    /**
     * 用户中心的用户修改的监听，
     * 一旦有用户修改，根据channel表中的开通情况，自动同步到对应的第三方服务
     *
     * @param tenantId
     * @param orgPk
     * @param ucUserDtoList
     */
    @Override
    public void modifyUserList(String tenantId, String orgPk, List<UcUserDto> ucUserDtoList) {
        List<BriChannel> briChannels = getChannels(tenantId,orgPk);
        briChannels.forEach(briChannel -> {
            if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
                String channel = briChannel.getChannel();
                switch (channel){
                    case "rtpnr":
                        iRtpnrUserService.updateUserByUserCenterListener(tenantId,orgPk,ucUserDtoList);
                        break;
                    case "clyh":
                        logger.info("开始向差旅一号更新同步用户：");
                        iclyhUserService.sysClyhUsers(tenantId, orgPk, ucUserDtoList, briChannel);
                        logger.info("结束向差旅一号更新同步用户：");
                        break;
                    case "ystrip":
                        break;
                }
            }
        });
    }

    /**
     * 用户中心的用户删除的监听，
     * 一旦有用户删除，根据channel表中的开通情况，自动在对应的第三方服务中删除用户
     *
     * @param tenantId
     * @param orgPk
     * @param userIdList
     */
    @Override
    public void deleteUserList(String tenantId, String orgPk, List<String> userIdList) {
        List<BriChannel> briChannels = getChannels(tenantId,orgPk);
        briChannels.forEach(briChannel -> {
            if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
                String channel = briChannel.getChannel();
                switch (channel){
                    case "rtpnr":
                        break;
                    case "ystrip":
                    case "clyh":
                        iclyhUserService.quitStaff(tenantId, orgPk, userIdList, briChannel);
                        break;
                }
            }
        });
    }

    /**
     * 获取开通渠道
     * @param tenantId
     * @param orgPk
     * @return
     */
    private List<BriChannel> getChannels(String tenantId, String orgPk){
        //首先查询已开通的channel
        Wrapper<BriChannel> wrapper = new EntityWrapper<>();
        wrapper.eq(BriChannel.TENANT,tenantId);
        wrapper.eq(BriChannel.ORGPK,orgPk);
        wrapper.eq(BriChannel.ENABLE,1);
        List<BriChannel> briChannels = iBriChannelService.selectList(wrapper);
        if (briChannels == null || briChannels.size() == 0){
            throw new BaseException(-100,"该租户未开通任何商旅服务");
        }
        return briChannels;
    }
}
